#ifndef AER_SCREENQUAD_HPP_
#define AER_SCREENQUAD_HPP_

#include "../aerOpenGL.hpp"


namespace aer {

// Actually, it does not draw a screen quad but 3 random points.
// Only useful with the appropriate vertex shader to create a triangle mapped 
// to fit the screen as a quad.
class ScreenQuad
{
  private:
    struct VAO_t
    {
      VAO_t() : vao(0u) {glGenVertexArrays( 1, &vao);}
      ~VAO_t() {if (vao) glDeleteVertexArrays( 1, &vao);}
      
      void begin() { glBindVertexArray( vao ); }
      void end() { glBindVertexArray( 0u ); }
      GLuint vao;
    };
      

  public:
    /**
     * VertexShader use to generate a triangle from 3 random vertices wheres
     * texCoord are computed to map the screen as a quad.
     */
    static const char* GetVertexShader()
    {
      return AER_STRINGIFY(
        \x23 version 330\n
        
        out vec2 vTexCoord;
        
        void main(void)
        {
          vTexCoord.s = (gl_VertexID << 1) & 2;
          vTexCoord.t = gl_VertexID & 2;
          
          gl_Position = vec4( 2.0f*vTexCoord - 1.0f, 0.0f, 1.0f);
        }
      );
    }
    
    static const char* GetFragmentShader()
    {
      return AER_STRINGIFY(
        \x23 version 330\n
        
        in vec2 vTexCoord;
        out vec4 fragColor;
        uniform sampler2D uTex;
        
        void main(void)
        {
          fragColor = texture( uTex, vTexCoord);
        }
      );
    }
    
    /** Send 3 random vertices to the GPU */
    static void Draw()
    {
      static VAO_t sVAO;
      
      sVAO.begin();
        glDrawArrays( GL_TRIANGLES, 0, 3);
      sVAO.end();
    }
};

} // aer

#endif // AER_SCREENQUAD_HPP_
